(function() {
  'use strict';

  angular
    .module('hp')
    .config(routerConfig)
    .factory('myInterceptor',myInterceptor)
    .service('client',getClient);

  /** @ngInject */
  function routerConfig($stateProvider, $urlRouterProvider,$httpProvider) {
    $stateProvider
        // .state('root', {
        //     url: '/',
        //     controller: 'RootController',
        //     controllerAs: 'root'
        // })
        .state('home', {
            url: '/',
            templateUrl: 'app/main/main.html',
            controller: 'MainController',
            controllerAs: 'main'
        })
		.state('machinecode',{
			url:'/machinecode',
			templateUrl:'app/machinecode/machinecode.html',
			controller:'MachineCodeController',
            controllerAs: 'machinecode'
		})
		.state('uploadcode',{
			url:'/uploadcode',
			templateUrl:'app/uploadcode/uploadcode.html',
			controller:'MachineCodeUploadController',
            controllerAs: 'uploadcode'
		})
		.state('playcode',{
			url:'/playcode',
			templateUrl:'app/playcode/playcode.html',
			controller:'PlayCodeController',
            controllerAs: 'playcode'
		})
		.state('login',{
			url:'/login',
			templateUrl:'app/login/login.html',
			controller:'LoginController',
            controllerAs: 'login'
		})
        .state('register',{
			url:'/register',
			templateUrl:'app/register/register.html',
			controller:'RegisterController',
            controllerAs: 'register'
		})
        .state('logout',{
			url:'/logout',
			controller:'LogoutController',
            controllerAs: 'logout'
		})
		.state('notfound',{
			url:'/404',
			templateUrl:'app/other/404.html'
		});

    $urlRouterProvider.otherwise('/');

    $httpProvider.interceptors.push('myInterceptor');
  }

  function getClient(){
      var ua = navigator.userAgent;
      var isIos = /iPhone|iPad|iPod/ig.test(ua) && !window.MSStream;
      var isAndroid = /Android/i.test(ua);
      // var web = !/MicroMessenger/ig.test(ua) && (!isIos && !isAndroid);
      return {source:isAndroid?0:isIos?1:2};
  }

  function myInterceptor($rootScope, $q,client,MyAuth){
      /* http状态码处理401和403返回的请求 */
      return {
          'request':function(config){
              config.headers['Content-Type'] = 'application/json';
              config.headers['source'] = '' + client.source;
              config.headers['token'] = '' + (MyAuth.loggedIn() || '');
            //   console.log(config);
            //   console.log(JSON.stringify(client));
              if(config.data){
                  config.data.head = {source:client.source,token:MyAuth.loggedIn() || ''}
              }
              return config;
          },
          'requestError':function(rejection){
              return $q.reject(rejection);
          },
          'response':function(response){
              if(response.status == 401 || response.status === 403) {
                  $rootScope.$broadcast('event:auth-require',response);
                  return $q.reject(response);
              }else{
                  if(response.head && response.head.errorCode == '401'){
                      return $q.reject(response.head);
                  }
                  return response;
              }
          },
          'responseError':function(rejection){
              if(rejection.status === 401 || rejection.status === 403){
                  $rootScope.$broadcast('event:auth-require',rejection);
              }else{
                  return $q.reject(rejection);
              }
          }
      };
  }

})();
